<?php
namespace App\Common\Server\Api\Gateway\ApiRequest;

use App\Common as com;
use App as nameApp;
use App\Common\Server\Api as nameApi;
use App\Common\Server\Api\Gateway as nameGateway;
use App\Common\Server\Api\Gateway\ApiRequest as nameApiRequest;
use App\Common\Server\Api\Gateway\ApiRequest\ApiData as nameApiData;

class ApiData extends nameApi\Base {

	/**
	 * 默认数据->版本号
	 * @var int
	 */
	public static int $varDefaultVersion = 1;

	/**
	 * 默认数据->签名类型
	 * @var int
	 */
	public static int $varDefaultSignType = 1;

	/**
	 * 1 : 签名类型->md5
	 * @var int
	 */
	public static int $varSignType_Md5 = 1;

	/**
	 * @var nameApiData\ApiParam
	 */
	private nameApiData\ApiParam $_apiParam;

	/**
	 * API前缀
	 * @var string
	 */
	private string $_apiPrefix = '';

	/**
	 * api数据
	 * @var array
	 */
	private array $apiData = [
		/*
		'accessToken' => '', // accessToken
		'requestId' => '', // requestId
		'timestamp' => '', // 时间戳
		'sign' => '', // 签名
		//
		'api' => '', // api名
		'version' => 1, // api版本号(默认1)
		'apiParam' => [], // api参数
		*/
	];

	/**
	 * 获取数据
	 * @param string|null $name [{string}]获取指定值 [{null}]获取所有
	 * @param mixed $default 默认值 [null][默认]
	 * @return mixed|array|null
	 */
	public function get( string $name = null, mixed $default = null ):mixed {
		if( null !== $name ){
			return $this->apiData[ $name ] ?? $default;
		}else{
			return $this->apiData;
		}
	}

	/**
	 * 设置数据
	 * @param array|string $value1 [{string}]设置指定值 [{array}]覆盖数据
	 * @param mixed $value2
	 * @return static
	 * @throws
	 */
	public function set( array|string $value1 = [], mixed $value2 = null ):static {
		if( is_array( $value1 ) ){
			$this->apiData = $value1;
		}elseif( null !== $value2 ){
			$this->apiData[ $value1 ] = $value2;
		}else{
			throw com\Lib\Err\Exception::makeOfNew( '', com\Lib\Err\Code::$sysFunParamError );
		}
		//
		return $this;
	}

	/**
	 * 获取API前缀
	 * @return string
	 */
	public function getApiPrefix():string {
		return $this->_apiPrefix;
	}

	/**
	 * 设置API前缀
	 * @param string $apiPrefix
	 * @return self
	 */
	public function setApiPrefix( string $apiPrefix ):self {
		$this->_apiPrefix = $apiPrefix;
		return $this;
	}

	/**
	 * 获取完整api名
	 * @return string
	 */
	public function getApiNameFull():string {
		return $this->getApiPrefix() . $this->apiData['api'];
	}

	/**
	 * 匹配api名前缀
	 * @param string $prefix
	 * @return string
	 */
	public function isApiNameMatchPrefix( string $prefix ):string {
		return str_starts_with( $this->getApiName(), $prefix );
	}

	/**
	 * 获取api名
	 * @return string
	 */
	public function getApiName():string {
		return $this->apiData['api'];
	}

	/**
	 * 设置api名
	 * @param string $apiName
	 * @return static
	 */
	public function setApi( string $apiName ):static {
		$this->apiData['api'] = $apiName;
		//
		return $this;
	}

	/**
	 * 获取api版本号
	 * @return int
	 */
	public function getVersion():int {
		return $this->apiData['version'] ?? self::$varDefaultVersion;
	}

	/**
	 * 设置api版本号
	 * @param int $apiVersion
	 * @return static
	 */
	public function setVersion( int $apiVersion ):static {
		$this->apiData['version'] = $apiVersion;
		//
		return $this;
	}

	/**
	 * 获取api accessToken
	 * @return string
	 */
	public function getAccessToken():string {
		return $this->apiData['accessToken'];
	}

	/**
	 * 设置api accessToken
	 * @param string $accessToken
	 * @return static
	 */
	public function setAccessToken( string $accessToken ):static {
		$this->apiData['accessToken'] = $accessToken;
		//
		return $this;
	}

	/**
	 * 获取requestId
	 * @return string
	 */
	public function getRequestId():string {
		return $this->apiData['requestId'];
	}

	/**
	 * 设置requestId
	 * @param string $requestId
	 * @return static
	 */
	public function setRequestId( string $requestId ):static {
		$this->apiData['requestId'] = $requestId;
		//
		return $this;
	}

	/**
	 * 获取isIndependentRes
	 * @return int
	 */
	public function getIsIndependentRes():int {
		return $this->apiData['isIndependentRes'] ?? 0;
	}

	/**
	 * 获取timestamp
	 * @return int
	 */
	public function getTimestamp():int {
		return $this->apiData['timestamp'];
	}

	/**
	 * 设置timestamp
	 * @param int $timestamp
	 * @return static
	 */
	public function setTimestamp( int $timestamp ):static {
		$this->apiData['timestamp'] = $timestamp;
		//
		return $this;
	}

	/**
	 * 获取sign
	 * @return string
	 */
	public function getSign():string {
		return $this->apiData['sign'];
	}

	/**
	 * 设置sign
	 * @param string $sign
	 * @return static
	 */
	public function setSign( string $sign ):static {
		$this->apiData['sign'] = $sign;
		//
		return $this;
	}


	/**
	 * 获取signType
	 * @return int
	 */
	public function getSignType():int {
		return $this->apiData['signType'] ?? self::$varDefaultSignType;
	}

	/**
	 * 设置signType
	 * @param int $signType
	 * @return static
	 */
	public function setSignType( int $signType ):static {
		$this->apiData['signType'] = $signType;
		//
		return $this;
	}

	/**
	 * @return nameApiData\ApiParam
	 */
	public function apiParam(  ):nameApiData\ApiParam {
		if( ! isset( $this->_apiParam ) ){
			$this->_apiParam = nameApiData\ApiParam::make();
		}
		//
		return $this->_apiParam;
	}

	/**
	 * 设置多参数apiParams
	 * @param array $apiParams
	 * @return static
	 */
	public function setApiParams( array $apiParams ):static {
		$this->apiData['apiParams'] = $apiParams;
		//
		return $this;
	}

	/**
	 * 是否为多参数请求
	 * @return bool
	 */
	public function isApiParams(  ):bool {
		return ! empty( $this->apiData['apiParams'] );
	}

	/**
	 * 获取多参数
	 * @return nameApiData\ApiParam[]
	 * @throws
	 */
	public function getApiParamsObj(  ):array {
		if( ! $this->isApiParams() ){
			return [];
		}
		//
		$apiParamObj = [];
		foreach( $this->apiData['apiParams'] as $apiParamsKey => $apiParamsValue ){
			$apiParamObj[ $apiParamsKey ] = nameApiData\ApiParam::make()->set( $apiParamsValue );
		}
		//
		return $apiParamObj;
	}

}
